﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace HCMServices.DAO
{
    [Table(Name = "TUKHOATINHTHANH")]
    public class KeyCityDAO
    {
        [Column(Name = "MaTuKhoaTinhThanh", IsDbGenerated = true, IsPrimaryKey = true, DbType = "int")]
        public int MaTuKhoaTinhThanh { get; set; }

        [Column(Name = "TuKhoaTinhThanh", DbType = "nvarchar(64)")]
        public string TuKhoaTinhThanh { get; set; }

        [Column(Name = "MaTinhThanh", DbType = "int")]
        public int MaTinhThanh { get; set; }

        private MyDataContext db;

        public KeyCityDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        public List<KeyCityDTO> Search(string strInput)
        {
            List<KeyCityDTO> lstTuKhoaTinhThanhDTO = new List<KeyCityDTO>();

            IList<TUKHOATINHTHANH> query = (from TUKHOATINHTHANH tktt in db.KeyCity
                                            where tktt.TuKhoaTinhThanh1 == strInput
                                            select tktt).ToList();

            foreach (TUKHOATINHTHANH tktt in query)
            {
                if (tktt.MaTinhThanh != null)
                {
                    KeyCityDTO temp = new KeyCityDTO();

                    temp.CityId = (int)tktt.MaTinhThanh;                    // tt.MaTinhThanh maybe null
                    temp.KeyCity = tktt.TuKhoaTinhThanh1;
                    temp.KeyCityId = tktt.MaTuKhoaTinhThanh;

                    lstTuKhoaTinhThanhDTO.Add(temp);
                }
            }

            return lstTuKhoaTinhThanhDTO;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tktt in db.KeyCity
                         from tinh in db.City
                         where tktt.MaTinhThanh == tinh.MaTinhThanh
                         select new { tktt.TuKhoaTinhThanh1, tinh.TenTinhThanh }).ToList();

            foreach (var t in query)
            {
                if (t.TuKhoaTinhThanh1 != null && !result.ContainsKey(t.TuKhoaTinhThanh1))
                    result.Add(t.TuKhoaTinhThanh1, t.TenTinhThanh);
            }

            return result;
        }
    }
}
